<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<c:set var="ctx" value=" ${pageContext.request.contextPath}" />
<html>
<head>
<title>人员信息表</title>
<script src="${ctx}/static/selectWin/employeeSelector.js?version=${applicationScope.jsVersion }" type="text/javascript"></script>
<script src="${ctx}/static/selectWin/orgFilterSelector.js?version=${applicationScope.jsVersion }" type="text/javascript"></script>
<script src="${ctx}/static/selectWin/fndPostSelector.js?version=${applicationScope.jsVersion }" type="text/javascript"></script>
<script type="text/javascript">
	var $grid;
	var $tree;
	var selectTreeNode;
	var $employeeSelector;
	var $orgSelector;
	var $fndPostSelector;
	var searchType=0;
	$(function() {
		 $tree = $("#tree").tree({
			method : "get",
			lines : true,
			url : "${ctx}/fnd/org/treeFilter?search_EQ_enable=1",
			dataPlain : true, //该属性用以启用当前 easyui-tree 控件对平滑数据格式的支持
			toggleOnClick : false,
			onSelect : function(node) {
				selectTreeNode = node;
				refreshEmployee();
				searchType=0;
			}

		});
		$grid = $("#grid").datagrid(
				$.extend(datagridOption, {
					//url : "${ctx}/fnd/employee/dataGrid",
					frozenColumns : [ [ {
							field : 'ck',
							checkbox : true
						}/* , {
							field : 'post',
							title : '岗位',
							width : 100,
							sortable : true,
							formatter : function(value, row, index) {
								return value ? value.postName : "";
							}
						} */, {
							field : 'trueName',
							title : '姓名',
							width : 100,
							sortable : true
						} ] ],
					columns : [ [ {
						field : 'jobNumber',
						title : '工号',
						width : 100,
						sortable : true
					}, {
						field : 'org',
						title : '所属组织',
						width : 200,
						sortable : true,
						formatter : function(value, row, index) {
							return value ? value.orgName : "";
						}
					},
					{
						field : 'enable',
						title : '是否启用',
						formatter : function(value, row, index) {
							return '<input class="easyui-switchbutton" ' + (value == 1 ? "checked" : "")
									+ ' data-options="readonly:true,onText:\'是\',offText:\'否\'">';
						}
					},
					{
						field : 'orderNo',
						title : '排序序号',
						sortable : true
					} ] ],
					onDblClickRow : function(index, rowData) {
						$.isFunction(fn_edit) && fn_edit.call(this, rowData);
					}
				}));
		$orgSelector = $("#org").comboselector(orgSelectOpts);
		$employeeSelector = $("#leader").comboselector(employeeSelectOpts);
		$fndPostSelector = $("#post").comboselector(fndPostSelectOpts);
		
	
	//$grid.datagrid('doFilter');

		$("#enableSB").switchbutton({
			onText : "是",
			offText : "否",
			checked : true,
			onChange : function(checked) {
				if (checked) {
					$("#enable").val(1);
				} else {
					$("#enable").val(0);
				}
			}
		});


		$("#add").click(function() {
			$("#fm").form("clear");
			$('#fm').form('load', {
				isPartTime : 0
			});
			$("#enableSB").switchbutton('check');
			$("#dlg").dialog("open").dialog("setTitle", "新增人员信息表");
		});
		$("#edit").click(function() {
			var rowData = $grid.datagrid("getSelected");
			if (!rowData) {
				showInfo("您未选中行，无法编辑");
			} else {
				fn_edit(rowData);
			}
		});
		$("#delete").click(function() {
			multiDeleteDataGrid($grid, "${ctx}/fnd/employee/multiDelete");
		});
		$("#search").click(function() {
			$grid.datagrid('load', $("#tt").form("getData"));
		});
		$("#excelImport").click(function() {
			$.easyui.importor.showImportor({
				importNo : "employee",
				onExportSuccess : function(file, data) {
					$grid.datagrid('load');
				}
			});
		});
		$("#excelExport").click(function() {
			var url = "${ctx}/fnd/employee/excelExport?" + $.param($grid.datagrid('options').queryParams);
			if ($grid.datagrid("options").filterRules) {
				url += "&filterRules=" + JSON.stringify($grid.datagrid("options").filterRules);
			}			
			if(searchType==1){
				url+="&search_IN_org.id=" + selectTreeNode.id
			}else if(searchType==0)
			{
				url+="&search_EQ_org.id=" + selectTreeNode.id
			}
			window.parent.location.href = url;
		});
		
		/*分配用户*/
		$("#fpuser").click(function(){
			var rows = $grid.datagrid('getChecked');
			var ids = "";
			if (rows) {
				$.each(rows, function(index, value) {
					ids += (value.id + ",")
				});
				if (ids == "") {
					$.messager.alert("操作提醒", "请先勾选需要分配账号的员工", "warning");
				} else {
					$.messager.confirm("您确定对勾选的" + rows.length + "个员工分配登陆账号吗？", function(c) {
						if (c) {
							$.post("${ctx}/fnd/employee/fpUser", {
								ids : ids
							}, function(result) {
								showInfo(result.msg);
								if (result.success) {
									$grid.datagrid("load").datagrid("clearSelections").datagrid("clearChecked");

								}
							}, 'json');
						}
					});
				}
			} else {
				$.messager.alert("操作提醒", "请先勾选需要分配账号的员工", "warning");
			}
		});
		
		$("#searchAll").click(function() {
			$grid.datagrid({
				url : "${ctx}/fnd/employee/dataGrid"
			});
			datagridField();
			selectTreeNode=null;
			searchType=2;
			
		});
		
		$("#searchChildrenEmp").click(function() {
			if (!selectTreeNode) {
				showAlert("请先选择部门");
				return false;
			}
			$grid.datagrid({
				url : "${ctx}/fnd/employee/dataGridChilrenEmp?search_IN_org.id=" + selectTreeNode.id
			});
			datagridField();
			searchType=1;
		});
	});
	//刷新人员管理
	function refreshEmployee() {
		if (!selectTreeNode) {
			showAlert("请先选择部门");
			return false;
		}
		$grid.datagrid('loadData', {
			total : 0,
			rows : []
		});
		$grid.datagrid({
			url : "${ctx}/fnd/employee/dataGrid?search_EQ_org.id=" + selectTreeNode.id
		});
		
		datagridField();
	}
	
	function datagridField(){
		$grid.datagrid('removeFilterRule');
		
		$grid.datagrid('enableFilter', [ {
			field : 'org',
			searchField : 'org.orgName'
		}, {
			field : 'post',
			searchField : 'post.postName'
		}, {
			field : 'enable',
			type : 'combobox',
			options : {
				panelHeight : 'auto',
				data : [ {
					value : ' ',
					text : '全部',
					selected : true
				}, {
					value : '1',
					text : '是'
				}, {
					value : '0',
					text : '否'
				} ],
				onChange : function(value) {
					if (value == '') {
						$grid.datagrid('removeFilterRule', 'enable');
					} else {
						$grid.datagrid('addFilterRule', {
							field : 'enable',
							op : 'EQ',
							value : value
						});
					}
					$grid.datagrid('doFilter');
				}
			}
		} ]);
		
		/* $grid.datagrid('addFilterRule', {
			field : 'enable',
			op : 'EQ',
			value : '1'
		}); */
	}
	
	<shiro:hasPermission name="employee:update">
	function fn_edit(rowData) {
		$("#dlg").dialog("open").dialog("setTitle", "编辑人员信息表");
		$("#fm").form("clear");
		$("#fm").form("load", rowData);
		$("#enableSB").switchbutton(rowData.enable == '1' ? "check" : "uncheck");
		rowData.org && $("#org").comboselector("setValue", rowData.org.id).comboselector("setText", rowData.org.orgName);
		rowData.post && $("#post").comboselector("setValue", rowData.post.id).comboselector("setText", rowData.post.trueName);
	}
	</shiro:hasPermission>
</script>
</head>

<body class="easyui-layout">
	<div data-options="region:'west',title:'组织机构',split:true" style="width: 400px;">
		<ul id="tree">
		</ul>
	</div> 
	<div data-options="region:'center',border:false">
		<table id="grid">
		</table>
		<div id="tt" class="row">	
			<div class="col-md-12">
				<shiro:hasPermission name="employee:allQuery">
				<button id="searchAll" type="button" class="btn btn-primary">
					<span class="glyphicon glyphicon-pencil"></span> 查看所有启用人员
				</button>
				</shiro:hasPermission>
				<!-- <button id="searchChildrenEmp" type="button" class="btn btn-primary">
					<span class="glyphicon glyphicon-pencil"></span> 查看子部门人员
				</button> -->
				<shiro:hasPermission name="employee:update">
					<button id="add" type="button" class="btn btn-primary">
						<span class="glyphicon glyphicon-plus"></span> 新增
					</button>
					<button id="edit" type="button" class="btn btn-primary">
						<span class="glyphicon glyphicon-pencil"></span> 编辑
					</button>
				</shiro:hasPermission>
				<shiro:hasPermission name="employee:delete">
					<button id="delete" type="button" class="btn btn-danger">
						<span class="glyphicon glyphicon-minus"></span> 删除
					</button>
				</shiro:hasPermission>
				<shiro:hasPermission name="conductTerm:import">
					<button id="excelImport" type="button" class="btn btn-primary">
						<span class="glyphicon glyphicon-open"></span> Excel导入
					</button>
				</shiro:hasPermission>
				<shiro:hasPermission name="employee:export">
					<button id="excelExport" type="button" class="btn btn-primary">
						<span class="glyphicon glyphicon-save"></span> Excel导出
					</button>
				</shiro:hasPermission>
				<shiro:hasPermission name="employee:update">
				<button id="fpuser" type="button" class="btn btn-primary">
						<span class="glyphicon glyphicon-open"></span> 分配登陆账号
					</button>
				</shiro:hasPermission>
			</div>
		</div>
		<shiro:hasPermission name="employee:update">
			<div id="dlg" class="easyui-dialog" style="width: 200px%; height: 300px%; padding: 10px 20px"
				data-options="resizable:true,modal:true, buttons:'#dlg-btn',closed:true">
				<form id="fm" method="post">
					<input type="hidden" name="id" />
					<table id="tbl">
						<tr>
							<td><label>工号:</label></td>
							<td><input name="jobNumber" class="easyui-validatebox" data-options="required: true"></td>
							<td><label>姓名:</label></td>
							<td><input name="trueName" class="easyui-validatebox" data-options="required: true"></td>
						</tr>
						<tr>
							<td><label>所属组织:</label></td>
							<td><input id="org" name="org" class="easyui-validatebox" data-options=""></td>
							<!-- <td><label>岗位:</label></td>
							<td><input id="post" name="post" class="easyui-validatebox" data-options=""></td> -->
	
						</tr>
						
						<tr>
							<td><label>是否启用:</label></td>
							<td><input id="enable" name="enable" type="hidden"> <input id="enableSB"></td>
							<td><label>员工排序号:</label></td>
							<td><input id="orderNo" name="orderNo" class="easyui-numberbox" data-options=""></td>
						</tr>
					</table>
				</form>
				<div id="dlg-btn">
					<button id="add" type="button" class="btn btn-primary" onclick="formSave('#fm', '#grid', '#dlg', '${ctx}/fnd/employee/update');">
						<span class="glyphicon glyphicon-ok"></span> 保存
					</button>
					<button id="edit" type="button" class="btn btn-primary" onclick="$('#dlg').dialog('close');">
						<span class="glyphicon glyphicon-remove"></span> 取消
					</button>
				</div>
			</div>
		</shiro:hasPermission>
	</div>
</body>
</html>
